<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>信息流管理</el-breadcrumb-item>
      <el-breadcrumb-item>平台管理</el-breadcrumb-item>
    </el-breadcrumb>

    <Container class="u-mb15">
      <!-- 筛选 -->
      <el-form
        inline
        class="m-form--filter"
      >
        <el-form-item>
          <el-select
            class="u-w160"
            size="mini"
            filterable
            placeholder="请选择平台"
            v-model="filter.platformId"
          >
            <el-option
              v-for="item in platformOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            @click="search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </Container>

    <Container>
      <!-- 表格操作 -->
      <el-form
        inline
        class="m-table--handle"
      >
        <el-form-item>
          <el-button
            size="mini"
            type="primary"
            @click="$router.push({
              name: 'flow-platform-detail',
              query: {
                action: 'create'
              }
            })"
          >新增</el-button>
        </el-form-item>
        <CustomTableColumn :columns.sync="tableColumn" />
      </el-form>
      <!-- 表格 -->
      <el-table
        border
        stripe
        :data="tableData"
      >
        <el-table-column
          align="center"
          v-for="(item, index) in filterTableColumn"
          :label="item.label"
          :key="index"
        >
          <template slot-scope="scope">
            <!-- 账号网址 -->
            <template v-if="item.prop === 'generalWebsite'">
              <span
                class="u-cursor"
                @click="openWindow(scope.row.generalWebsite)"
              >{{scope.row.generalWebsite}}</span>
            </template>
            <!-- 是否启用 -->
            <template v-else-if="item.prop === 'status'">
              <el-switch
                :width="34"
                :value="scope.row.status"
                :active-value="2"
                :inactive-value="1"
                @change="changeStatus(scope.row.id, scope.row.status === 1 ? 2 : 1)"
              ></el-switch>
            </template>
            <!-- 其他 -->
            <template v-else>{{scope.row[item.prop]}}</template>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-platform-detail',
                query: {
                  action: 'edit',
                  id: scope.row.id
                }
              })"
            >编辑</el-button>
            <el-button
              type="text"
              @click="$router.push({
                name: 'flow-platform-detail',
                query: {
                  action: 'detail',
                  id: scope.row.id
                }
              })"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        :page-sizes="[50, 100, 500, 1000]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="pageTotal"
        @size-change="handlePagerSizeChange"
        @current-change="handlePagerCurrentChange"
      />
    </Container>
  </div>
</template>

<script>
import Container from "@/components/container";
import CustomTableColumn from "@/components/custom-table-column";

export default {
  name: "flow-platform",
  components: {
    Container,
    CustomTableColumn
  },
  data() {
    return {
      // 平台名称
      platformOptions: [],

      // 筛选
      filter: {
        platformId: null
      },

      tableData: [],
      tableColumn: [
        {
          label: "平台名称",
          prop: "platformName",
          visible: true
        },
        {
          label: "普通账号登录网址",
          prop: "generalWebsite",
          visible: true
        },
        {
          label: "是否启用",
          prop: "status",
          visible: true
        }
      ],
      page: {
        pageNo: 1,
        pageSize: 50
      },
      pageTotal: 0
    };
  },
  computed: {
    filterTableColumn() {
      return this.tableColumn.filter(item => item.visible);
    }
  },
  methods: {
    fetchPlatformOptions() {
      this.$http({
        method: "get",
        url: "/manage/api/bus/platform/get_platform",
        params: {
          productId: 0,
          customerId: 0,
          status: 0
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.platformOptions = [
            {
              id: null,
              name: "全部平台"
            }
          ].concat(res.data.data);
        }
      });
    },
    search() {
      this.page.pageNo = 1;
      this.getTable();
    },
    getTable() {
      this.$http({
        method: "post",
        url: "/manage/api/bus/platform/get_list",
        data: {
          ...this.filter,
          ...this.page
        }
      }).then(res => {
        if (res.data.code === 0) {
          let data = res.data.data;
          this.tableData = data.list;
          this.pageTotal = data.listCount;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    changeStatus(id, status) {
      this.$http({
        method: "post",
        url: "/manage/api/bus/platform/status",
        params: {
          ids: id,
          status
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.$message.success(`${status === 1 ? "停用" : "启用"}成功！`);
          this.getTable();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    openWindow(url) {
      if (url) {
        window.open(url);
      }
    },
    handlePagerSizeChange(v) {
      this.page.pageSize = v;
      this.getTable();
    },
    handlePagerCurrentChange(v) {
      this.page.pageNo = v;
      this.getTable();
    }
  },
  activated(){
    this.getTable();
  },
  created() {
    this.fetchPlatformOptions();
  }
};
</script>